package com.game.base.utils.time;

import com.game.base.app.App;
import com.game.base.utils.psp.PSP;

/**
 * 时间间隔判断
 */
public class IntervalUtil {
    /**
     * 自动检查更新间隔， 限制调用次数
     */
    private static final String CHECK_UPDATE_KEY = "check_update_key";

    private static String getCheckUpdateKey() {
        return CHECK_UPDATE_KEY;
    }

    public static boolean needCheckUpdate() {
        long currentTime = System.currentTimeMillis();
        long time = PSP.getInstance().getLong(getCheckUpdateKey(), 0);
        long distance = (currentTime - time) / 1000;

        // 是否超过半天
        boolean check = distance > 60 * 60 * 6;
        if (check) {
            updateCheckDate();
        }
        return check;
    }

    private static void updateCheckDate() {
        PSP.getInstance().put(getCheckUpdateKey(), System.currentTimeMillis());
    }

    /**
     * 服务器时间： 判断是否到达第二天并进行时间存储:  签到，重置今日收入等
     *
     * @return true 达到第二天
     */
    private static final String CHECK_OPENID_KEY = "check_openid_key";

    private static String getCheckOpenidKey() {
        return CHECK_OPENID_KEY;
    }

    public static boolean checkOpenidDate() {
        String recommendDate = PSP.getInstance().getString(getCheckOpenidKey(), "-1");
        return !recommendDate.equals(TimeUtil.formatTime(App.time, FormatPattern.FP_YMD2));
    }

    public static void updateCheckOpenidDate() {
        PSP.getInstance().put(getCheckOpenidKey(), TimeUtil.formatTime(App.time,
                FormatPattern.FP_YMD2) + "");
    }
}
